package com.sky.service.impl;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 用户端删除购物车里的一个菜品或者套餐
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //购物车删除菜品或者套餐业务
        //1.每个用户只能查询自己的购物车数据
        //2.用户提交过来的要么是菜品id，要么是套菜id
        //3.减少一个直接减少number数量如果number数量为0就从购物车中删除该菜品或者套餐
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置当前购物车为哪个用户
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品或者套餐是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        // 如果购物车中没有该数据直接返回
        if (CollectionUtils.isEmpty(shoppingCartList) || shoppingCartList.size() != 1) {
            return;
        }
        ShoppingCart existingCart = shoppingCartList.get(0);
        Integer currentNumber = existingCart.getNumber();

        // 数量为1时删除商品
        if (currentNumber == 1) {
            Long dishId = existingCart.getDishId();
            // 使用三目运算符简化单条件判断
            if (dishId != null) {
                shoppingCartMapper.deleteByDishId(dishId);
            } else {
                shoppingCartMapper.deleteBySetmealId(existingCart.getSetmealId());
            }
            return; // 提前返回，避免else
        }
        // 数量大于1时更新数量
        existingCart.setNumber(currentNumber - 1);
        shoppingCartMapper.update(existingCart);
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //添加购物车业务
        //1.每个用户只能查询自己的购物车数据
        //2.用户提交过来的要么是菜品id，要么是套菜id
        //3.对同一个菜品或者套餐不需要添加多条数据，只需要增加number数量即可
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置当前购物车为哪个用户
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品或者套餐是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //如果购物车中已经存在，则更新数量
        if (shoppingCartList != null && shoppingCartList.size() == 1){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //否则数量为1, 根据菜品id或者套餐id
            //如果添加的是菜品
            if(shoppingCartDTO.getDishId()!=null){
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                //如果添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 用户清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }
}
